<!DOCTYPE html>
<head>
    @include('web.layout.head')
    <link rel="stylesheet" href="{{asset('/css/web/order/submin_cart_kp.css')}}">
</head>

<body>
@include('web.layout.header')
@include('web.layout.user_tab')
<div class="base_member_content">
    <div class="w1200">
        @include('web.layout.buyer_nav')
        <div class="nav_right flr">
            <div class="submit-order-wrap">
                <div class="invoice-wrap">

                    <div class="invoice">
                        <div class="invoice_name">发票信息</div>
                        <div class="invoice_fenlei">
                            @if (empty($invoice['invoice_message'])||$invoice['invoice_message'] == 1)
                                <?php $invoice_message = 1; ?>
                            @endif

                            @if (!empty($invoice['invoice_type']) && $invoice['invoice_message'] != 1)
                                @if ($invoice['invoice_type'] == 1)
                                    <?php $invoice_message = 3; ?>
                                @else
                                    <?php $invoice_message = 4; ?>
                                @endif
                            @endif
                            <div class="person fenlei geren j-bill  {{$invoice_message==1?'hover':''}}" data-type="1">
                                个人
                                <span class="i" style="{{$invoice_message==1?'display:inline;':''}}" ></span>
                            </div>
                            <div class="company fenlei danwei j-bill {{$invoice_message!=1?'hover':''}}" data-type="2">单位<span class="i" style="{{$invoice_message!=1?'display:inline;':''}}"></span></div>

                        </div>
                    </div>
                    <div class="invoice_taitou" style="{{$invoice_message!=1?'display:none;':''}}">
                        <div class="name">发票抬头</div>
                        <input type="text" id="billheader" name="billheader" placeholder="请输入发票抬头信息"
                               value="{{array_get($invoice,'invoice_message')==1?array_get($invoice,'invoice_title'):''}}"/>
                    </div>
                    <div class="zengzhi_invoice" style="{{$invoice_message!=1?'display:inline;':'display: none;'}}">
                        <div class="invoice">
                            <div class="invoice_name leixing">发票类型</div>
                            <div class="invoice_fenlei">
                                <div class="person fenlei j-bill-type person_ordinary {{(3 == $invoice_message)? 'hover':''}}" data-type="3">增值普通发票<span class="i" style="{{(3 == $invoice_message)? 'display:inline;':''}}"></span></div>
                                <div class="company fenlei j-bill-type {{(4 == $invoice_message)? 'hover':''}}" data-type="4">增值专用发票<span class="i" style="{{(4 == $invoice_message)? 'display:inline;':''}}"></span></div>
                            </div>
                        </div>
                        <div class="invoice_dt">
                            <ul>
                                <li>
                                    <div>发票抬头：</div>
                                    <input type="text" name="ubillheader" id="ubillheader"
                                           value="{{array_get($invoice,'invoice_message')==2?array_get($invoice,'invoice_title'):''}}"/>
                                </li>
                                <li>
                                    <div>发票税号：</div>
                                    <input type="text" name="tax_code" id="tax_code"
                                           value="{{array_get($invoice,'invoice_message')==2?array_get($invoice,'tax_code'):''}}"/>
                                </li>
                                <li>
                                    <div>开票地址：</div>
                                    <input type="text" name="tax_addr" id="tax_addr"
                                           value="{{array_get($invoice,'invoice_message')==2?array_get($invoice,'invoice_address'):''}}"/>
                                </li>
                                <li>
                                    <div>开票电话：</div>
                                    <input type="text" onkeyup="this.value = this.value.replace(/\D/g, '')"
                                           name="tax_tel" id="tax_tel" value="{{array_get($invoice,'invoice_message')==2?array_get($invoice,'invoice_tel'):''}}"/>
                                </li>
                                <li>
                                    <div>开票开户银行：</div>
                                    <input type="text" name="tax_bank" id="tax_bank"
                                           value="{{array_get($invoice,'invoice_message')==2?array_get($invoice,'invoice_bank'):''}}"/>
                                </li>
                                <li>
                                    <div>开票银行账户：</div>
                                    <input type="text" onkeyup="this.value = this.value.replace(/\D/g, '')"
                                           name="tax_account" id="tax_account" value="{{array_get($invoice,'invoice_message')==2?array_get($invoice,'invoice_account'):''}}"/>
                                </li>
                                <li>
                                    <div>收票人姓名：</div>
                                    <input type="text" name="billname" maxlength="20" id="billname"
                                           value="{{array_get($invoice,'invoice_message')==2?array_get($invoice,'post_contacts'):''}}"/>
                                </li>
                                <li>
                                    <div>收票人手机：</div>
                                    <input type="text" onkeyup="this.value = this.value.replace(/\D/g, '')"
                                           name="billphone" id="billphone" value="{{array_get($invoice,'invoice_message')==2?array_get($invoice,'post_tel'):''}}"/>
                                </li>
                                <li>
                                    <div>详细地址：</div>
                                    <input type="text" name="billaddr" id="billaddr"
                                           value="{{array_get($invoice,'invoice_message')==2?array_get($invoice,'post_address'):''}}"/>
                                </li>
                            </ul>
                        </div>
                    </div>
                    <div class="tips">温馨提示：发票的开票金额仅限实际支付金额（不包含积分抵扣等优惠部分）。每月25日（含25日）之后的付款订单，将在下月开具发票</div>
                </div>
                <div class="baocun">保存发票信息</div>
                <input hidden id="user_invoice_id"
                       value="{{array_get($invoice,'id')}}"/>
                <input hidden id="data_type" value="{{$invoice_message}}"/>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function () {

        var checkData = '';
        var  u_id= $("#user_invoice_id").val();
        $(".fenlei").click(function () {
            $(this).addClass("hover").siblings().removeClass("hover");
            $(this).find(".i").show();
            $(this).siblings().find(".i").hide();
            var data_type = '';
            var type = $(this).attr('data-type');
            if (1 == type) {
                $(".person_ordinary").addClass("hover").siblings().removeClass("hover");
                $(".invoice_fenlei .i").hide();
                $(".person_ordinary .i").show();
                data_type = 1;
            } else {
                data_type = type;
            }

            if(data_type==3||data_type==4){
                var sendParams = data_type==3?'normal':false;
                sendParams = sendParams==false?(data_type==4?'professional':false):sendParams;
                checkClickInvoices(sendParams);
            }
            $("#data_type").val(data_type);
        })
        $(".geren").click(function () {
            $(".zengzhi_invoice").hide();
            $(".invoice_taitou").show();
            checkClickInvoices('person');
        })
        $(".jm_btn5").click(function () {
            $(".overlay,.mb-box").hide();
        })
        $(".danwei").click(function () {
            $(".invoice_taitou").hide();
            $(".zengzhi_invoice").show();
        })

        $(".baocun").click(function () {
            var user_invoice_id = $("#user_invoice_id").val();
            var data = {};
            var data_type = $("#data_type").val();
            //个人
            if (0 == data_type || 1 == data_type) {
                if ($("#billheader").val() == '') {
                    layer.alert('发票抬头不能为空', {icon: 0});
                    $("#ubillheader").focus();
                    return false;
                }
                data = {
                    '_token': "{{csrf_token()}}",
                    'invoice_message': 1,
                    'invoice_type': 1,
                    'invoice_title': $("#billheader").val()
                };
            } else {
                //单位
                if ($("#ubillheader").val() == '') {
                    layer.alert('发票抬头不能为空', {icon: 0});

                    $("#ubillheader").focus();
                    return false;
                }

                if ($("#tax_code").val() == '') {
                    layer.alert('发票税号不能为空', {icon: 0});

                    $("#tax_code").focus();
                    return false;
                }
                if (!$("#tax_code").val().match(/^[A-Za-z0-9]+$/)) {
                    layer.alert('发票税号格式不对(只能填写英文和数字)', {icon: 0});
                    $("#billname").focus();
                    return false;
                }
                if ($("#tax_addr").val() == '') {
                    layer.alert('开票地址不能为空', {icon: 0});

                    $("#tax_addr").focus();
                    return false;
                }
                if ($("#tax_tel").val() == '') {
                    layer.alert('开票电话不能为空', {icon: 0});

                    $("#tax_tel").focus();
                    return false;
                }
                if (!$("#tax_tel").val().match(/^1[34578]\d{9}$/) && !$("#tax_tel").val().match(/^(?:(?:0\d{2,3}))?(?:\d{7,8})(-(?:\d{3,}))?$/)) {
                    layer.alert('开票电话格式有误', {icon: 0});

                    $("#tax_tel").focus();
                    return false;
                }
                if ($("#tax_bank").val() == '') {
                    layer.alert('开票开户银行不能为空', {icon: 0});

                    $("#tax_bank").focus();
                    return false;
                }

                if ($("#tax_account").val() == '') {
                    layer.alert('开票银行账户不能为空', {icon: 0});

                    $("#tax_account").focus();
                    return false;
                }
                if ($("#billname").val() == '') {
                    layer.alert('收票人姓名不能为空', {icon: 0});
                    $("#billname").focus();
                    return false;
                }
                if (!$("#billname").val().match(/^[\u4e00-\u9fa5a-zA-Z]+$/)) {
                    layer.alert('收票人姓名格式不对(只能填写中文和英文)', {icon: 0});
                    $("#billname").focus();
                    return false;
                }
                if ($("#billphone").val() == '') {
                    layer.alert('收票人手机不能为空', {icon: 0});
                    $("#billphone").focus();
                    return false;
                }
                if (!$("#billphone").val().match(/^1[34578]\d{9}$/)) {
                    layer.alert('收票人手机格式有误', {icon: 0});
                    $("#billphone").focus();
                    return false;
                }
                if ($("#billaddr").val() == '') {
                    layer.alert('详细地址不能为空', {icon: 0});
                    $("#billaddr").focus();
                    return false;
                }
                var invoice_type = 1;
                if (4 == data_type) {
                    invoice_type = 2;
                }
                data = {
                    '_token': "{{csrf_token()}}",
                    'invoice_message': 2,
                    'invoice_type': invoice_type,
                    'invoice_title': $("#ubillheader").val(),
                    'tax_code': $("#tax_code").val(),
                    'invoice_address': $("#tax_addr").val(),
                    'invoice_tel': $("#tax_tel").val(),
                    'invoice_bank': $("#tax_bank").val(),
                    'invoice_account': $("#tax_account").val(),
                    'post_address': $("#billaddr").val(),
                    'post_contacts': $("#billname").val(),
                    'post_tel': $("#billphone").val(),
                };
            }
            if (user_invoice_id) {
                data.id = user_invoice_id;
            }
            $.ajax({
                url: '/member/buyer/invoices',
                type: 'POST',
                dataType: 'json',
                data: data,
                success: function (data) {
                    if (data.code == 0) {
                        layer.alert('保存成功', {
                            title: '温馨提示',
                            btn: ['确认'] //可以无限个按钮
                        }, function (index) {
                            location.href = '/member/buyer/invoices';
                        });
                    } else {
                        layer.alert(data.message, {icon: 2});
                    }
                }
            });

        });
        console.log(typeof u_id);
        // 检测是否可以添加发票
        checkInvoices();
        function checkInvoices(){
            var flag = false;
            var token = "{{csrf_token()}}";
            var msg = '您已添加：';
            $.ajax({
                url: '/member/buyer/checkInvoice',
                type: 'GET',
                dataType: 'json',
                data: {},
                success: function (res) {
                    checkData = res;
                    if (res.data.all == true) {
                        flag = true;
                        if(u_id==''){
                            layer.confirm('每种类型的发票只能添加一张，点击确认返回发票列表', {
                                    btn: ['确认'] //可以无限个按钮
                                }, function (index) {
                                    /* Act on the event */
                                    window.location.href = '/member/buyer/invoices';
                                }
                            );
                        }
                    }else{
                        if(res.data.person == true){
                            msg += '【个人发票】';
                        }
                        if(res.data.companyNormal == true){
                            msg += '【单位普通发票】';
                        }
                        if(res.data.companyProfessional == true){
                            msg += '【单位专用发票】';
                        }
                        if(msg.length>5){
                            layer.confirm('每种类型的发票只能添加一张，<br>'+msg+'<br>点击确认继续添加', {
                                        btn: ['确认'] //可以无限个按钮
                                    }, function (index) {
                                        /* Act on the event */
                                        layer.close(index);
                                    }
                            );
                        }
                    }

                }
            });
        }

        function checkClickInvoices(params){
            // 单击个人发票
            if(params=='person'){
                if (checkData.data.person == true && checkData.data.all==false) {
                    layer.confirm('每种类型的发票只能添加一张，您已添加【个人发票】类型，点击确认继续添加。', {
                            btn: ['确认'] //可以无限个按钮
                        }, function (index) {
                            /* Act on the event */
                            layer.close(index);
                            $('.danwei').trigger('click');
                        }
                    );
                }else if(checkData.data.person == true && checkData.data.all==true ) {
                    layer.confirm('每种类型的发票只能添加一张，您已添加所有发票类型，点击确认返回发票列表。', {
                            btn: ['确认'] //可以无限个按钮
                        }, function (index) {
                            /* Act on the event */
                            window.location.href = '/member/buyer/invoices';
                        }
                    );
                }
            // 单击单位普通发票
            }else if(params=='normal'){
                if (checkData.data.companyNormal == true && checkData.data.all==false) {
                    layer.confirm('每种类型的发票只能添加一张，您已添加【普通发票】类型，点击确认继续添加其他类型发票。', {
                            btn: ['确认'] //可以无限个按钮
                        }, function (index) {
                            /* Act on the event */
                            layer.close(index);
                            if(checkData.data.companyProfessional == false){
                                $('.zengzhi_invoice .company').trigger('click');
                            }
                            if(checkData.data.person == false){
                                $('.geren').trigger('click');
                            }

                        }
                    );
                }else if(checkData.data.companyNormal == true && checkData.data.all==true ){
                    layer.confirm('每种类型的发票只能添加一张，您已添加所有发票类型，点击确认返回发票列表。', {
                            btn: ['确认'] //可以无限个按钮
                        }, function (index) {
                            /* Act on the event */
                            window.location.href = '/member/buyer/invoices';
                        }
                    );
                }
            // 单击单位专用发票
            }else{ // professional
                if (checkData.data.companyProfessional == true && checkData.data.all==false) {
                    layer.confirm('每种类型的发票只能添加一张，您已添加【专用发票】类型，点击确认继续添加其他类型发票。', {
                            btn: ['确认'] //可以无限个按钮
                        }, function (index) {
                            /* Act on the event */
                            layer.close(index);
                            if(checkData.data.companyNormal == false){
                                $('.zengzhi_invoice .person').trigger('click');
                            }
                            if(checkData.data.person == false){
                                $('.geren').trigger('click');
                            }
                        }
                    );
                }else if(checkData.data.companyProfessional == true && checkData.data.all==true ){
                    layer.confirm('每种类型的发票只能添加一张，您已添加所有发票类型，点击确认返回发票列表。', {
                            btn: ['确认'] //可以无限个按钮
                        }, function (index) {
                            /* Act on the event */
                            window.location.href = '/member/buyer/invoices';
                        }
                    );
                }
            }
        }
    })

</script>
</body>
</html>